package com.appspot.didit.test.service;

import java.util.Date;
import java.util.ArrayList;

import javax.jdo.PersistenceManager;
import com.appspot.didit.client.workitem.Workitem;
import com.appspot.didit.server.WorkitemServiceImpl;
import com.appspot.didit.server.PMF;
import com.appspot.didit.test.LocalServiceTestCase;

public class WorkitemServiceTestCase extends LocalServiceTestCase {
	
	private WorkitemServiceImpl wsi = new WorkitemServiceImpl();
	PersistenceManager pm = PMF.get().getPersistenceManager();
	
	public void testSave() {
	    try {
	    	//Initiation
	    	/*Identity id = new Identity("12345678901234567");
			pm.makePersistent(id);
			Long user_id = id.getId();
			//******************************
			Date date = new Date();
	    	Workitem wi1 = new Workitem("test", 1.0, "tag", date, user_id);
			Workitem wi2 = wsi.save(wi1);
			Workitem wi3 = pm.getObjectById(Workitem.class, wi2.getId());
	    	assertNotNull(wi3);
			assertEquals("test", wi3.getDescription());
			assertEquals(1.0, wi3.getDuration());	
			assertEquals("tag", wi3.getTagString());
			assertEquals(date, wi3.getDate());
			assertEquals(user_id, wi3.getUser_id());*/
	    } catch (Exception e) {
	    	System.out.println(e.toString());
	    	assertTrue(false);
	    } finally {
	      pm.close();
	    }
	}
	
	public void testUpdate() {
		try {
	    	//Initiation
	    	/*Identity id = new Identity("12345678901234567");
			pm.makePersistent(id);
			Long user_id = id.getId();
			//******************************
			Date date = new Date();
			Date date1 = new Date(0L);
	    	Workitem wi1 = new Workitem("test", 1.0, "tag", date, user_id);
	    	pm.makePersistent(wi1);
	    	Workitem wi2 = pm.detachCopy(wi1);
			wi2.setDescription("test1");
			wi2.setTagString("tag1 tag2");
			wi2.setDuration(2.0);
			wi2.setDate(date1);
			wsi.update(wi2);
			ArrayList<Workitem> workitems = wsi.loadWorkitems(user_id);
			Workitem wi3 = workitems.get(0);//pm.getObjectById(Workitem.class, wi2.getId());
			assertEquals(wi2.getId(), wi3.getId());
			assertEquals("test1", wi3.getDescription());
			assertEquals(2.0, wi3.getDuration());	
			assertEquals("tag1 tag2", wi3.getTagString());
			assertEquals(date1, wi3.getDate());*/
	    } catch (Exception e) {
	    	System.out.println(e.toString());
	    	assertTrue(false);
	    } finally {
	      pm.close();
	    }
	}
	
	public void testLoadWorkitems() {
		try {
	    	//Initiation
	    	/*Identity id = new Identity("12345678901234567");
			pm.makePersistent(id);
			Long user_id = id.getId();
			//******************************
			Date date = new Date();
			for(int i = 0; i < 10; i++) {
				Workitem wi = new Workitem("test"+i, i, "tag"+i, date, user_id);
				pm.makePersistent(wi);
			}
			ArrayList<Workitem> workitems = wsi.loadWorkitems(user_id);
	    	assertNotNull(workitems);
	    	assertEquals(10, workitems.size());*/
	    } catch (Exception e) {
	    	System.out.println(e.toString());
	    	assertTrue(false);
	    } finally {
	      pm.close();
	    }
	}
}
